package com.xiaoxin.suma.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.xiaoxin.suma.R;
import com.xiaoxin.suma.activity.LoginActivity;
import com.xiaoxin.suma.net.Api;
import com.xiaoxin.suma.net.ErrUtils;
import com.xiaoxin.suma.utils.ActivityManager;
import com.xiaoxin.suma.utils.DataManger;
import com.xiaoxin.suma.utils.RxUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class UserFragment extends BaseFragment {

    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_jifen)
    TextView tvJifen;
    @BindView(R.id.tv_yve)
    TextView tvYve;
    @BindView(R.id.tv_num)
    TextView tvNum;
    Unbinder unbinder;
    @BindView(R.id.tv_status)
    TextView tvStatus;

    public UserFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    protected void initView() {
        super.initView();
        Api.service().getUserInfo(userName, Token)
                .compose(RxUtil.rxSchedulerio_main())
                .subscribe(this::paseData, ErrUtils::pase);
    }

    private void paseData(String s) {
        boolean flag = false;
        switch (s) {
            case "parameter_error":
                s = "传入参数错误";
                out();
                break;
            case "not_login":
                s = "有登录,在没有登录下去访问需要登录的资源，忘记传入uid,token";
                out();
                break;
            case "account_is_locked":
                s = "账号被锁定";
                tvStatus.setText(s);
                break;
            case "account_is_stoped":
                s = "账号被停用";
                tvStatus.setText(s);
                break;
            case "account_is_question_locked":
                s = "账号已关闭";
                tvStatus.setText(s);
                break;
            case "account_is_ip_stoped":
                s = "账号ip锁定";
                tvStatus.setText(s);
                break;
            case "message|please try again later":
                s = "访问速度过快，建议休眠50毫秒后再试";
                break;
            case "unknow_error":
                s = "未知错误,再次请求就会正确返回";
                out();
                break;
            default:
                flag = true;
                String[] strings = s.trim().split(";");
                tvName.setText(strings[0]);
                tvJifen.setText(strings[1]);
                tvYve.setText(String.format("%.2f元", Float.parseFloat(strings[2]) / 100f));
                tvNum.setText(strings[3]);
                tvStatus.setText("正常");
                break;

        }
        if (!flag) {
            ToastUtils.showShort(s);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.bt_out)
    public void out() {
        DataManger.out();
        ActivityManager.getInstance().finishAll();
        startActivity(new Intent(mContext, LoginActivity.class));
    }
}
